package com.azier.main;

import com.azier.exception.ServiceException;
import com.azier.influxdb.mapper.InfluxDBBaseMapper;
import com.azier.influxdb.model.MonitoringStatsDataInfluxdb;
import com.azier.mysql.model.MonitoringStatsDataDay;
import com.azier.mysql.model.TransportResourceCore;
import com.azier.mysql.service.MonitoringStatsDataDayService;
import com.azier.mysql.service.TransportResourceCoreService;
import com.azier.util.CollectionUtil;
import com.azier.util.NumberUtil;
import com.azier.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.influxdb.impl.InfluxDBMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1 * @Author: luoj
 * 2 * @Date: 2019/11/20 10:19
 * 3
 */
@Setter
@Getter
@Component
public class MainBean {
    @Value("${influxDB.host:@null}")
    private String influxDBHost;

    @Autowired
    private MonitoringStatsDataDayService monitoringStatsDataDayService;
    @Autowired
    private TransportResourceCoreService transportResourceCoreService;
    @Autowired
    private InfluxDBBaseMapper influxDBBaseMapper;


    private static final Map<String,Map<Long, TransportResourceCore>> coreData = new HashMap<>();

    //查询总数
    private int selectCount() throws ServiceException {
        return monitoringStatsDataDayService.selectCount();
    }


    //分页查询
    public void processDayData(){
        try {
            int count = selectCount();
            int z = count%20;
            int allPage = count/20 + (z>0?1:0);
            for (int i=1;i<=allPage;i++){
                List<MonitoringStatsDataDay> datas = monitoringStatsDataDayService.selectStatDataPage(i,20);
                if (CollectionUtil.isNotEmpty(datas)) {
                    for (MonitoringStatsDataDay monitoringStatsDataDay:datas){
                        if(monitoringStatsDataDay!=null){
                            TransportResourceCore core = jetTransportResourceCore(monitoringStatsDataDay.getDeviceId(),monitoringStatsDataDay.getMonitoringId());
                            if(core!=null){
                                saveMonitoringStatsDataInfluxdb(core,monitoringStatsDataDay);
                                System.out.println(i);
                            }
                        }
                    }
                }
            }
        } catch (ServiceException e) {
            System.out.println(e.getMessage());
        }

    }


    //处理每一条
    //按照deviceId->smid查询 1.查内存，2，查询数据库
    private TransportResourceCore jetTransportResourceCore(String deviceId,Long monitoringId) throws ServiceException {
        Map<Long,TransportResourceCore> innerData = coreData.get(monitoringId);
        TransportResourceCore core;
        if(CollectionUtil.isNotEmpty(innerData)){
            core = innerData.get(monitoringId);
            if(core!=null){
                return core;
            }
        }else {
            innerData = new HashMap<>();
            coreData.put(deviceId,innerData);
        }
        Example example = new Example(TransportResourceCore.class);
        example.createCriteria().andEqualTo("deviceId",deviceId).andEqualTo("monitoringId",monitoringId);
        core = transportResourceCoreService.selectOneByExample(example);
        if(core!=null){
            innerData.put(monitoringId,core);
        }
        return core;
    }

    //拼接数据

    private void saveMonitoringStatsDataInfluxdb(TransportResourceCore transportResourceCore, MonitoringStatsDataDay dataDay){
        MonitoringStatsDataInfluxdb monitoringStatsDataInfluxdb = new MonitoringStatsDataInfluxdb();
        monitoringStatsDataInfluxdb.setDeviceId(transportResourceCore.getDeviceId());
        monitoringStatsDataInfluxdb.setTenantId(StringUtil.toStringOrEmpty(transportResourceCore.getTenantId()));
        monitoringStatsDataInfluxdb.setMonitoringCode(StringUtil.toStringOrEmpty(transportResourceCore.getMonitoringCode()));
        monitoringStatsDataInfluxdb.setAreaCode(StringUtil.toStringOrEmpty(transportResourceCore.getRegionCode()));
        monitoringStatsDataInfluxdb.setStationId(StringUtil.toStringOrEmpty(transportResourceCore.getStationId()));
        monitoringStatsDataInfluxdb.setSmCode(StringUtil.toStringOrEmpty(transportResourceCore.getSmCode()));
        monitoringStatsDataInfluxdb.setAdvStationEquipmentId(StringUtil.toStringOrEmpty(transportResourceCore.getAdvStationEquipmentId()));
        monitoringStatsDataInfluxdb.setIndexId(StringUtil.toStringOrEmpty(transportResourceCore.getIndexId()));
        monitoringStatsDataInfluxdb.setDataType(StringUtil.toStringOrEmpty(transportResourceCore.getDataType()));
        monitoringStatsDataInfluxdb.setValueType(StringUtil.toStringOrEmpty(transportResourceCore.getValueType()));
        monitoringStatsDataInfluxdb.setEquipmentSequence(StringUtil.toStringOrEmpty(transportResourceCore.getEquipmentSequence()));
        monitoringStatsDataInfluxdb.setMonitoringSequence(StringUtil.toStringOrEmpty(transportResourceCore.getMonitoringSequence()));
        monitoringStatsDataInfluxdb.setTempId(StringUtil.toStringOrEmpty(transportResourceCore.getTempId()));
        monitoringStatsDataInfluxdb.setProcessType("1");
        monitoringStatsDataInfluxdb.setDate(dataDay.getDataTime());
        monitoringStatsDataInfluxdb.setValue(NumberUtil.round(dataDay.getDataValue(),2));
        influxDBBaseMapper.save2PublicDatabase(monitoringStatsDataInfluxdb);
    }
}
